package android.goalkeeper;

import android.app.Activity;
import android.os.Bundle;
import android.utils.GameController;
import android.utils.GameControllerTouchListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import opengl.OpenGLRenderer;

public class MainActivity extends Activity {

    private GameController mOpenglView;
    private View.OnTouchListener mTouchListener;
    private OpenGLRenderer mRenderer;
    private GameThread mMainThread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        //TouchListener
        mTouchListener = new GameControllerTouchListener();
//        //SurfaceView
        mOpenglView = new GameController(this, mTouchListener);
        mMainThread = new GameThread(mOpenglView);
        mRenderer = new OpenGLRenderer(mMainThread);
        mOpenglView.setRenderer(mRenderer);
        setContentView(mOpenglView);
        //Bind TouchListener
        mOpenglView.setOnTouchListener(mTouchListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            mMainThread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            mMainThread.setRunning(false);
            mMainThread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            mMainThread.setRunning(false);
            mMainThread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
